package baseTest;

import java.util.ArrayList;
import java.util.List;

public class spiralOrder {
    public static void main(String[] args) {

    }

    public List<Integer> process(int[][] matrix){
        List<Integer> list = new ArrayList<>();
        int tR = 0;
        int tC = 0;
        int dR = matrix.length-1;
        int dC = matrix[0].length-1;
        while (tR <= dR && tC <= dC){
            printEdge(matrix,tR++,tC++,dR--,dC--,list);
        }
        return list;
    }

    public void printEdge(int[][] m,int tR,int tC,int dR,int dC,List<Integer> list){
        if (tR == dR){
            for (int i = tC; i < dC; i++) {
                list.add(m[tR][i]);
            }
        } else if (tC == dC){
            for (int i = tR; i <= dR; i++) {
                list.add(m[i][tC]);
            }
        } else {
            int curC = tC;
            int curR = tR;
            while (curC !=dC){
                list.add(m[tR][curC]);
                curC++;
            }
            while (curR != dR){
                list.add(m[curR][dC]);
                curR++;
            }
            while (curC!=tC){
                list.add(m[dR][curC]);
                curC--;
            }
            while (curR!=tR){
                list.add(m[curR][tC]);
                curR--;
            }
        }
    }

}
